package com.furns.dao.impl;

import com.furns.dao.BasicDAO;
import com.furns.dao.FurnDAO;
import com.furns.entity.Furn;

import java.util.List;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {
    /**
     * 返回所有的家具信息集合
     * @return
     */
    @Override
    public List<Furn> queryFurns() {
        String sql= "select id,name,maker,price,sales,stock,img_path as imgPath from furn";
        return queryMulti(sql,Furn.class);
    }
    /**
     * 添加家居
     * @return
     */
    @Override
    public int addFurn(Furn furn) {
        String sql = "insert into furn(id,name,maker,price,sales,stock,img_path) values(null,?,?,?,?,?,?)";
        int update = update(sql,furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(),furn.getImgPath());
        return update;
    }
    /**
     * 删除家居
     * @return
     */
    @Override
    public int deleteFurnById(int id) {
        String sql = "delete from furn where id=?";
        int update = update(sql,id);
        return update;
    }
    /**
     * 根据id查找家居
     * @param id
     * @return 根据id查找的家居
     */
    @Override
    public Furn queryFurnById(int id) {
        String sql = "select id,name,maker,price,sales,stock,img_path as imgPath from furn where id=?";
        Furn furn = querySingle(sql, Furn.class, id);
        return furn;
    }
    /**
     * 修改家居
     * @param furn
     * @return
     */
    @Override
    public int updateFurn(Furn furn) {
        String sql = "update furn set name=?,maker=?,price=?,sales=?,stock=?,img_path=? where id=?";
        int update = update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
        return update;
    }

    /**
     *
     * @return 数据库家居总数
     */
    @Override
    public int getTotalRow() {
        String sql = "select count(*) from furn";
        return ((Number)queryScalar(sql)).intValue();
    }

    /**
     *
     * @return 当前页面家居数据
     */
    @Override
    public List<Furn> getPageItems(int begin,int pageSize) {
        String sql = "select id,name,maker,price,sales,stock,img_path as imgPath from furn limit ?,?";
        return queryMulti(sql,Furn.class,begin,pageSize);
    }
    /**
     *
     * @param name
     * @return 该家居名的数据条数
     */
    @Override
    public int getPageTotalCountByName(String name) {
        String sql = "select count(*) from furn where name like ?";
        return ((Number)queryScalar(sql,"%"+name+"%")).intValue();
    }

    /**
     *
     * @param name
     * @param begin
     * @param pageSize
     * @return 该家具名的数据集合
     */
    @Override
    public List<Furn> getPageItemsByName(String name,int begin,int pageSize) {
        String sql = "select id,name,maker,price,sales,stock,img_path as imgPath from furn where name like ? limit ?,?";
        return queryMulti(sql,Furn.class,"%"+name+"%",begin,pageSize);
    }
}
